package com.kotlin.myapplication.mvp.ui.main

import android.annotation.SuppressLint
import android.view.KeyEvent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.kotlin.myapplication.R
import com.kotlin.myapplication.base.BaseActivity
import com.kotlin.myapplication.mananger.AppManager
import com.kotlin.myapplication.mvp.ui.main.fragment.ShopFragment
import com.kotlin.myapplication.mvp.ui.main.fragment.UserCenterFragment
import com.kotlin.myapplication.mvp.ui.main.adapter.ViewPagerAdapter
import com.kotlin.myapplication.mvp.ui.main.fragment.MyDevicesFragment
import com.kotlin.myapplication.mvp.ui.main.fragment.SquareFragment
import kotlinx.android.synthetic.main.activity_mainfirst.*


/**
 *@lil
 *@创建时间 2019/11/4 14:39
 *@描述0
 */
class MainfirstActivity : BaseActivity(), View.OnClickListener {

    var mFragmentList = ArrayList<Fragment>()

    var mViewPagerAdapter: ViewPagerAdapter? = null

    /**
     * 2次退出记录时间
     */
    private var secendBackTime: Long = 0

    /**
     * fragment position 标签
     */
    private val MY_DEVICES_POSITION = 0
//    private val SHOP_POSITION = 1
//    private val SQUARE_POSITION = 2
    private val USER_CENTER_POSITION = 1

    override fun initView() {
        setContentView(R.layout.activity_mainfirst)
        my_devices.setOnClickListener(this)
        shop.setOnClickListener(this)
        square.setOnClickListener(this)
        user_info.setOnClickListener(this)
        initViewPager()
    }

    private fun initViewPager() {

        main_gridview.setNoScroll(true)

        var myDevicesFragment = MyDevicesFragment()
        mFragmentList.add(myDevicesFragment)
//        var shopFragment = ShopFragment()
//        mFragmentList.add(shopFragment)
//        var squareFragment = SquareFragment()
//        mFragmentList.add(squareFragment)
        var userCenterFragment = UserCenterFragment()
        mFragmentList.add(userCenterFragment)

        mViewPagerAdapter = ViewPagerAdapter(supportFragmentManager, mFragmentList, null)
        main_gridview.adapter = mViewPagerAdapter
        main_gridview.offscreenPageLimit = 2
        var onPageChangeListener = object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(pos: Int) {
//                if (pos == SHOP_POSITION) {
//                    main_botton_menu.visibility = View.GONE
//                } else {
//                    main_botton_menu.visibility = View.VISIBLE
//                }
            }
        }
        main_gridview.addOnPageChangeListener(onPageChangeListener)
    }

    private fun changeView(index: Int) {
        main_gridview.setCurrentItem(index, true);
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.my_devices -> {
                changeView(MY_DEVICES_POSITION)
            }
//            R.id.shop -> {
//                changeView(SHOP_POSITION)
//            }
//            R.id.square -> {
//                changeView(SQUARE_POSITION)
//            }
            R.id.user_info -> {
                changeView(USER_CENTER_POSITION)
            }
        }
    }

    fun turnToDevicesFragment() {
        /**
         * 未登录时，默认显示广场
         */
        my_devices.isChecked = true
        changeView(MY_DEVICES_POSITION)

    }

    @SuppressLint("RestrictedApi")
    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        return if (event.getKeyCode() === KeyEvent.KEYCODE_BACK && event.getAction() === KeyEvent.ACTION_DOWN) {
            //忽略所有返回键按下事件
            doSecendBack()
            true
        } else {
            super.dispatchKeyEvent(event)
        }
    }


    /**
     * 2次退出
     */
    private fun doSecendBack() {
        var currentTime = System.currentTimeMillis()
        if (currentTime - secendBackTime > 2000) {
            secendBackTime = currentTime
            showToast(resources.getString(R.string.app_exit_hint))
        } else {
            AppManager.finishAllActivity()
        }
    }
}
